package cn.edu.cug.springwebdemo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.SearchResults;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/twitter")
public class TwitterController {
    private static final Logger logger = LoggerFactory.getLogger(TwitterController.class);

    @Autowired
    private Twitter twitter;

    @RequestMapping(value = "/search")
    public String search(@RequestParam(defaultValue = "defaultValue") String search, Model model){
        SearchResults searchResults = twitter.searchOperations().search(search);
        String text = searchResults.getTweets().get(0).getText();
        model.addAttribute("message", text);

        return "twitter/searchResult";
    }
}
